#!/bin/bash

#
# controlv -- by Dario Berzano <dario.berzano@gmail.com>
#
# Part of the lfscripts suite -- http://lfscripts.googecode.com/
#
# Unpacks the text output containing files obtained from controlc.
#

# Find correct base64
which gbase64 > /dev/null 2>&1
[ $? == 0 ] && B64=gbase64 || B64=base64

if [ $# != 0 ]; then
  echo "No arguments are required."
fi

T=$(mktemp /tmp/controlv-XXXXX)

echo "Start pasting under the following line, end with Control+D (EOF):"
echo "-----BEGIN PASTE HERE-----"
cat | "$B64" -d > $T
echo "-----END PASTE HERE-----"

echo "Files pasted:"
tar tjf $T | while read F
do
  echo -n " * $F"
  [ -e "$F" ] && echo -e " \033[31m(already exists)\033[m" || echo ""
done

echo -n "Unpack? "
read -n 1 R
echo ''
if [ "$R" == "y" ] || [ "$R" == "Y" ]; then
  tar xjf $T
else
  echo "Unpack cancelled"
fi

rm -f $T
